package com.huawei;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description: HJ45 名字的漂亮度
 *
 * 注意判空和0！！！
 * @author weiruibai.vendor
 * Date: 2022/8/29 10:26
 */
public class HJ45 {

    /**
     * 输入：
     * 2
     * zhangsan
     * lisi
     * 复制
     * 输出：
     * 192
     * 101
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int line = Integer.parseInt(str);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < line; i++) {
            list.add(br.readLine().toLowerCase());
        }
        for (String s : list) {
            Integer[] chars = new Integer[26];
            for (Character ch : s.toCharArray()) {
                chars[ch - 'a'] = (chars[ch - 'a'] == null ? 0 : chars[ch - 'a']) + 1;
            }
            int ans = 0;
            int base = 26;
            Arrays.sort(chars, (o1, o2) -> {
                // 注意判空
                if (o1 == null) {
                    o1 = 0;
                }
                if (o2 == null) {
                    o2 = 0;
                }
                return o2 - o1;
            });
            for (int i = 0; i < 26; i++) {
                // 注意要判断空和0！！！
                if (chars[i]==null || chars[i]==0){
                    continue;
                }
                ans += chars[i] * base;
                base--;
            }
            System.out.println(ans);
        }

    }
}
